package com.qingfeng.client.api;

import com.qingfeng.client.VO.LogisticsQueryVo;
import com.qingfeng.client.VO.OrderGoodsVo;
import com.qingfeng.client.VO.OrderStoreVo;
import com.qingfeng.client.VO.TransformFeeVo;
import com.qingfeng.client.bean.Buyer;
import com.qingfeng.client.bean.LogisticsCompany;
import com.qingfeng.client.bean.OrderStore;
import com.qingfeng.client.bean.ShippingAddress;
import com.qingfeng.client.dao.LogisticsCompanyMapper;
import com.qingfeng.client.dao.ShippingAddressMapper;
import com.qingfeng.client.service.OrderService;
import com.qingfeng.util.ApiMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/1/6 10:21 上午
 */
@Api(tags = "收货地址接口")
@RestController
@RequestMapping("/app/shippingAddress")
public class ShippingAddressApi extends BaseV1Api {

    @Resource
    private ShippingAddressMapper shippingAddressDao;

    @Resource
    private LogisticsCompanyMapper LogisticsCompanyDao;

    @Autowired
    private OrderService orderService;

    @ApiOperation("添加收货地址")
    @PostMapping("/add")
    public ApiMsg save(@RequestBody ShippingAddress shippingAddress) {
        Buyer buyer = getUser();
        shippingAddress.setBuyerId(buyer.getId());
        int i = 0;
        if (shippingAddress.getType() == 1) {
            shippingAddressDao.updateTypeByBuyerId(0, buyer.getId());
        }
        i = shippingAddressDao.insert(shippingAddress);

        if (i > 0) {
            return success();
        }
        return error();
    }


    @ApiOperation("更新收货地址")
    @PostMapping("/update")
    public ApiMsg update(@RequestBody ShippingAddress shippingAddress) {
        Buyer buyer = getUser();
        ShippingAddress shippingAddress1 = shippingAddressDao.selectById(shippingAddress.getId());
        if (shippingAddress1.getBuyerId().equals(buyer.getId())) {
            shippingAddress.setBuyerId(buyer.getId());
            if (shippingAddress.getType() == 1) {
                shippingAddressDao.updateTypeByBuyerId(0, buyer.getId());
            }
            shippingAddressDao.updateById(shippingAddress);
            return success();
        } else {
            return error("不能更新非本人的数据");
        }
    }

    @ApiOperation("删除收货地址")
    @PostMapping("/delete")
    public ApiMsg delete(@RequestParam String ids) {
        Buyer buyer = getUser();
        shippingAddressDao.deleteByBuyerIdAndId(buyer.getId(), ids);
        return success();
    }

    @ApiOperation("设置默认收货地址")
    @PostMapping("/default")
    public ApiMsg setDefault(@RequestParam long id) {
        Buyer buyer = getUser();
        ShippingAddress shippingAddress = shippingAddressDao.selectById(id);
        if (buyer.getId().equals(shippingAddress.getBuyerId())) {
            shippingAddress.setType(1);
            shippingAddressDao.updateTypeByBuyerId(0, buyer.getId());
            shippingAddressDao.updateById(shippingAddress);
            return success();
        } else {
            return error("不能更新非本人的数据");
        }
    }

    @ApiOperation("查询收货地址")
    @GetMapping("/query")
    public ApiMsg<List<ShippingAddress>> query() {
        Buyer buyer = getUser();
        List<ShippingAddress> list = shippingAddressDao.selectByBuyerId(buyer.getId());
        return success(list);
    }

    @ApiOperation("查询自提地址/物流公司名称")
    @PostMapping("/logisticsQuery")
    public ApiMsg<List<LogisticsCompany>> logisticsQuery(@RequestBody LogisticsQueryVo logisticsQueryVo) {
        List<LogisticsCompany> list =
                LogisticsCompanyDao.selectByStoreIdAndType(logisticsQueryVo.getStoreId(), logisticsQueryVo.getType());
        return success(list);
    }


    @ApiOperation("计算运费")
    @PostMapping("/getTransformFee")
    public ApiMsg<List<LogisticsCompany>> getTransformFee(@RequestBody TransformFeeVo vo) {
        orderService.getTransformFee(vo);



        return success();
    }


}
